package com.bj58.mis.apm.server.logreceiver.service.impl;

import com.bj58.mis.apm.server.logreceiver.dto.HostModel;
import com.bj58.mis.apm.server.logreceiver.model.HostCpucoreInfo;
import com.bj58.mis.apm.server.logreceiver.model.HostDfInfo;
import com.bj58.mis.apm.server.logreceiver.model.HostInfo;
import com.bj58.mis.apm.server.logreceiver.model.HostPsInfo;
import com.bj58.mis.apm.server.logreceiver.repository.HostLogRepository;
import com.bj58.mis.apm.server.logreceiver.service.HostLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.math.BigInteger;
import java.util.Map;

@Service
@Transactional
public class HostLogServiceImpl implements HostLogService {

    @Autowired
    private HostLogRepository hostLogRepository;



    @Override
    public void receive(HostModel model) {
        HostInfo hostInfo=new HostInfo();
        hostInfo.setAppName(model.getAppName());
        hostInfo.setHostName(model.getHostName());
        hostInfo.setCollectTime(new java.sql.Timestamp(model.getCollectTime()));

        //os信息
        hostInfo.setUserName(model.getUsername());
        hostInfo.setComputerName(model.getComputerName());
        hostInfo.setComputerHostName(model.getComputerHostName());
        hostInfo.setUserDomain(model.getUserDomain());
        hostInfo.setOsName(model.getOsname());
        hostInfo.setOsArch(model.getOsarch());
        hostInfo.setOsVersion(model.getOsversion());

        //cpu信息
        hostInfo.setCpuWait(model.getCpu().get("wait"));
        hostInfo.setCpuStolen(model.getCpu().get("stolen"));
        hostInfo.setCpuIdle(model.getCpu().get("idle"));
        hostInfo.setCpuSoftirq(model.getCpu().get("softIrq"));
        hostInfo.setCpuIrq(model.getCpu().get("irq"));
        hostInfo.setCpuCombined(model.getCpu().get("combined"));
        hostInfo.setCpuSys(model.getCpu().get("sys"));
        hostInfo.setCpuUser(model.getCpu().get("user"));
        hostInfo.setCpuNice(model.getCpu().get("nice"));

        //cpu型号信息
        hostInfo.setCpuTotalCores(Integer.parseInt(model.getCpuInfo().get("totalCpus")));
        hostInfo.setCpuVendor(model.getCpuInfo().get("vendor"));
        hostInfo.setCpuModel(model.getCpuInfo().get("model"));
        hostInfo.setCpuMhz(model.getCpuInfo().get("Mhz"));

        //内存信息
        hostInfo.setMemUsedPercent(model.getMem().get("usedPercent"));
        hostInfo.setMemTotal(model.getMem().get("total").longValue());
        hostInfo.setMemFreePercent(model.getMem().get("freePercent"));
        hostInfo.setMemActualUsed(model.getMem().get("actualUsed").longValue());
        hostInfo.setMemUsed(model.getMem().get("used").longValue());
        hostInfo.setMemFree(model.getMem().get("free").longValue());
        hostInfo.setMemActualFree(model.getMem().get("actualFree").longValue());

        //swap信息
        hostInfo.setSwapTotal(model.getSwap().get("total").longValue());
        hostInfo.setSwapPageIn(model.getSwap().get("pageIn").longValue());
        hostInfo.setSwapPageOut(model.getSwap().get("pageOut").longValue());
        hostInfo.setSwapUsed(model.getSwap().get("used").longValue());
        hostInfo.setSwapFree(model.getSwap().get("free").longValue());

        //网络信息
        hostInfo.setNetBroadcast(model.getNetworkconfig().get("broadcast"));
        hostInfo.setNetHwaddr(model.getNetworkconfig().get("hwaddr"));
        hostInfo.setNetAddress(model.getNetworkconfig().get("address"));
        hostInfo.setNetMask(model.getNetworkconfig().get("netmask"));
        hostInfo.setNetName(model.getNetworkconfig().get("name"));
        hostInfo.setNetDescription(model.getNetworkconfig().get("description"));
        hostInfo.setNetType(model.getNetworkconfig().get("type"));
        hostInfo.setNetRxPackets(model.getNetworkstat().get("rxPackets"));
        hostInfo.setNetRxBytes(model.getNetworkstat().get("rxBytes"));
        hostInfo.setNetRxPacketsPsec(model.getNetworkstat().get("rxPacketsPSec")==null?-1:model.getNetworkstat().get("rxPacketsPSec"));
        hostInfo.setNetRxBytesPsec(model.getNetworkstat().get("rxBytesPSec")==null?-1:model.getNetworkstat().get("rxBytesPSec"));
        hostInfo.setNetTxBytes(model.getNetworkstat().get("txBytes"));
        hostInfo.setNetTxPackets(model.getNetworkstat().get("txPackets"));
        hostInfo.setNetTxPacketsPsec(model.getNetworkstat().get("txPacketsPSec")==null?-1:model.getNetworkstat().get("txPacketsPSec"));
        hostInfo.setNetTxBytesPsec(model.getNetworkstat().get("txBytesPSec")==null?-1:model.getNetworkstat().get("txBytesPSec"));
        hostInfo.setNetSpeed(model.getNetworkstat().get("speed"));

        //进程汇总信息
        hostInfo.setProcRunning(Math.toIntExact(model.getProcStat().get("running")));
        hostInfo.setProcStopped(Math.toIntExact(model.getProcStat().get("stopped")));
        hostInfo.setProcTotal(Math.toIntExact(model.getProcStat().get("total")));
        hostInfo.setProcIdle(Math.toIntExact(model.getProcStat().get("idle")));
        hostInfo.setProcZombie(Math.toIntExact(model.getProcStat().get("zombie")));
        hostInfo.setProcThreads(Math.toIntExact(model.getProcStat().get("threads")));
        hostInfo.setProcSleeping(Math.toIntExact(model.getProcStat().get("sleeping")));

        //本进程信息
        hostInfo.setSelfPsRss(model.getSelfPs().get("rss"));
        hostInfo.setSelfPsSize(model.getSelfPs().get("size"));
        hostInfo.setSelfPsCpu(model.getSelfPs().get("cpu"));
        hostInfo.setSelfPsPid(model.getSelfPs().get("pid"));
        hostInfo.setSelfPsStime(model.getSelfPs().get("stime"));
        hostInfo.setSelfPsShare(model.getSelfPs().get("share"));
        hostInfo.setSelfPsState(model.getSelfPs().get("state"));
        hostInfo.setSelfPsTime(model.getSelfPs().get("time"));
        hostInfo.setSelfPsUser(model.getSelfPs().get("user"));
        hostInfo.setSelfPsCommand(model.getSelfPs().get("command"));

        //cpu各个核心的信息
        int cpuRn=1;
        for(Map<String,Double> data:model.getCpuCoreInfo()){
            HostCpucoreInfo core=new HostCpucoreInfo();
            core.setRn(cpuRn++);
            core.setCpuWait(data.get("wait"));
            core.setCpuStolen(data.get("stolen"));
            core.setCpuIdle(data.get("idle"));
            core.setCpuSoftirq(data.get("softIrq"));
            core.setCpuIrq(data.get("irq"));
            core.setCpuCombined(data.get("combined"));
            core.setCpuSys(data.get("sys"));
            core.setCpuUser(data.get("user"));
            core.setCpuNice(data.get("nice"));
            hostInfo.getHostcpucoreInfo().add(core);
        }

        //其他进程的具体信息
        for(Map<String,String> data:model.getPs())
        {
            HostPsInfo ps=new HostPsInfo();

            ps.setPsRss(data.get("rss"));
            ps.setPsSize(data.get("size"));
            ps.setPsCpu(data.get("cpu"));
            ps.setPsPid(data.get("pid"));
            ps.setPsStime(data.get("stime"));
            ps.setPsShare(data.get("share"));
            ps.setPsState(data.get("state"));
            ps.setPsTime(data.get("time"));
            ps.setPsUser(data.get("user"));
            ps.setPsCommand(data.get("command"));

            hostInfo.getHostPsInfo().add(ps);
        }

        //磁盘信息
        for(Map<String,String> data:model.getDf()){
            HostDfInfo df=new HostDfInfo();
            df.setName(data.get("name"));
            df.setTotal(Long.valueOf(data.get("total")));
            df.setUsed(Long.valueOf(data.get("used")));
            df.setFree(Long.valueOf(data.get("free")));
            df.setAvail(Long.valueOf(data.get("avail")));
            df.setUsePercent(Double.parseDouble(data.get("usePercent")));
            df.setDiskQueue(Double.parseDouble(data.get("diskQueue")));
            df.setDiskWrites(Long.valueOf(data.get("diskWrites")));
            df.setDiskWriteBytes(Long.valueOf(data.get("diskWriteBytes")));
            df.setDiskReads(Long.valueOf(data.get("diskReads")));
            df.setDiskReadBytes(Long.valueOf(data.get("diskReadBytes")));

            df.setDiskWritesPsec(data.get("diskWritesPSec")==null?-1: Long.parseLong(data.get("diskWritesPSec")));
            df.setDiskWriteBytesPsec(data.get("diskWriteBytesPSec")==null?-1: Long.parseLong(data.get("diskWriteBytesPSec")));
            df.setDiskReadsPsec(data.get("diskReadsPSec")==null?-1: Long.parseLong(data.get("diskReadsPSec")));
            df.setDiskReadBytesPsec(data.get("diskReadBytesPSec")==null?-1: Long.parseLong(data.get("diskReadBytesPSec")));

            hostInfo.getHostDfInfo().add(df);
        }

        hostLogRepository.save(hostInfo);
    }
}

